Quiz 7 (Week 7)
Table of Contents
1 Applicatives
1.1 Question 1
Which of the following type definitions are examples of Applicative
?
Maybe
String
(->) a
for anya
(,) a
for anya
[ ]
Gen
1.2 Question 2
Consider the following data type definition for a non-empty list in Haskell.
data NonEmptyList a = One a | Cons a (NonEmptyList a)
Which of the following are law-abiding Applicative
instances for NonEmptyList
?
instance Applicative NonEmptyList where pure x = Cons x (pure x) (One f) <*> (One x) = One (f x) (One f) <*> (Cons x _) = One (f x) (Cons f _) <*> (One x) = One (f x) (Cons f fs) <*> (Cons x xs) = Cons (f x) (fs <*> xs)
instance Applicative NonEmptyList where pure x = One x (One f) <*> (One x) = One (f x) (One f) <*> (Cons x _) = One (f x) (Cons f _) <*> (One x) = One (f x) (Cons f fs) <*> (Cons x xs) = Cons (f x) (fs <*> xs)
instance Applicative NonEmptyList where pure x = One x One f <*> xs = fmap f xs (Cons f fs) <*> xs = fmap f xs `append` (fs <*> xs) where append (One x) ys = Cons x ys append (Cons x xs) ys = Cons x (xs `append` ys)
instance Applicative NonEmptyList where pure x = Cons x (pure x) One f <*> xs = fmap f xs (Cons f fs) <*> xs = fmap f xs `append` (fs <*> xs) where append (One x) ys = Cons x ys append (Cons x xs) ys = Cons x (xs `append` ys)
1.3 Question 3
Suppose I wanted to write a function pair
, which takes two Applicative
data structures and combines them in a tuple, of type:
pair :: (Applicative f) => f a -> f b -> f (a, b)
Select all correct implementations of pair
.
pair fa fb = pure fa <*> pure fb
pair fa fb = pure (,) <*> pure fa <*> pure fb
pair fa fb = pure (,) <*> fa <*> fb
pair fa fb = fmap (,) fa <*> fb
- There is no way to implement this function.
2 Monads
2.1 Question 4
Which of the following type definitions are examples of Monad
, or admit
law-abiding Monad
instances?
Maybe
String
(->) a
for anya
(,) a
for anya
[ ]
Gen
2.2 Question 5
We wish to write a function s
of type [m a] -> m [a]
, for any monad m
.
It will unpack each given value of type m a
and collect their results into a list.
Which of the following is a correct implementation
of this function?
s :: Monad m => [m a] -> m [a] s [] = [] s (a:as) = do x <- a xs <- s as return (x : xs)
s :: Monad m => [m a] -> m [a] s [] = return [] s (a:as) = do x <- a xs <- as return (x : xs)
s :: Monad m => [m a] -> m [a] s [] = return [] s (a:as) = do x <- a xs <- s as return (x : xs)
s :: Monad m => [m a] -> m [a] s [] = return [] s (a:as) = do a s as return (a : as)
3 Functor, Monad, Applicative
3.1 Question 6
Which of the following are correct definitions of the usual <*>
operation
for the type [a]
?
fs <*> xs = do f <- fs x <- xs return (f x)
fs <*> xs = concatMap (flip map xs) fs
fs <*> xs = fs >>= \f -> xs >>= \x -> [f x]
fs <*> xs = fs >>= \f -> xs >>= \x -> f x
3.2 Question 7
Recall that two functions are considered equal if they have the same output for every input. Select all the true statements below.
- Kleisli composition is not well-defined in the
Maybe
monad. - Every monad
m
and every functionf :: a -> m a
satisfies the equalityf <=< f = f
. - Every instance of
Applicative
has to be an instance ofMonad
as well. - Every instance of
Monad
has to be an instance ofFunctor
as well.
3.3 Question 8
If a given unary type m
is a Monad
instance (and also a Functor
and a Applicative
instance), which of the following would
definitely be correct implementations of fmap :: (a -> b) -> m a -> m b
?
fmap f [] = [] fmap f (x:xs) = map f xs
fmap f = (pure f <*>)
fmap f xs = f <*> xs
fmap f xs = xs >>= \x -> return (f x)